package cs2340.todolist.model;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;

import com.google.android.maps.GeoPoint;

/**
 * Manages physical locations with an address and a name
 * @author BaoTam
 *
 */
public class Location {


	private String name;
	private String addr;
	

	public Location(String name, String addr) {
		
		this.name = name;
		this.setAddr(addr);
		
	}
	
	public Location(String name) {
		this.name = name;
		this.setAddr("");
	}
	
	public GeoPoint geocode(Context context) {
		
		Geocoder geoCoder = new Geocoder(context, Locale.getDefault());   
		
        try {
        	System.out.println("geocoding: " + addr);
            List<Address> addresses = geoCoder.getFromLocationName(addr, 5);
            System.out.println("found: " + addresses.size());
            if (addresses.size() > 0) {
                return new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), 
                        			 (int) (addresses.get(0).getLongitude() * 1E6));
                //mc.animateTo(p);
                //mapView.invalidate();
            }
            else {
            	return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return geocode(context);
        }
	}
	

	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getAddr() {
		return addr;
	}


	public void setAddr(String addr) {
		this.addr = addr;
	}
	
	public String toString() {
		return name + " - " + addr;
	}


	
}
